package com.easy.query.demo;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.demo.domain.HelpCode;
import com.fhs.trans.service.impl.DictionaryTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;

/**
 * create time 2023/9/22 22:55
 * 文件说明
 *
 * @author xuejiaming
 */
@Component
public class EasyTransRunner implements ApplicationRunner {
    @Autowired  //注入字典翻译服务
    private DictionaryTransService dictionaryTransService;
    @Autowired
    private EasyQuery easyQuery;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<HelpCode> list = easyQuery.queryable(HelpCode.class)
                .toList();
        Map<Integer, List<HelpCode>> collect = list.stream().collect(groupingBy(o -> o.getType()));
        for (Map.Entry<Integer, List<HelpCode>> integerListEntry : collect.entrySet()) {
            HashMap<String, String> transMap = new HashMap<>();
            for (HelpCode helpCode : integerListEntry.getValue()) {
                transMap.put(helpCode.getCode(),helpCode.getName());
            }

            dictionaryTransService.refreshCache(integerListEntry.getKey().toString(),transMap);
        }

    }
}
